<?php
class UrlRewrite {

    public function __construct() {
        $this->setting = $GLOBALS[ 'setting' ];
    }

    public function get_url( $c, $action = null, $args = null, $anchor = null ) {
        $var = array( '$tag', '$key', '$alias', '$id' );

        foreach ( $this->setting[ 'rewrite' ] as $k => $v ) {
            if ( $k == $c ) {
                $url = str_replace( array( '$y', '$m', '$d' ), array( date( 'Y', $args[ 'date' ] ), date( 'm', $args[ 'date' ] ), date( 'd', $args[ 'date' ] ) ), $v );
                foreach ( $var as $a ) {
                    $tmp = str_replace( '$', '', $a );
                    if ( isset( $args[ $tmp ] ) ) {
                        $url = str_replace( $a, $args[ $tmp ], $url );
                        break;
                    }
                }
                break;
            } else {
                $url = 'page';
            }
        }

        if ( $c == 'archives' && isset( $args[ 'alias' ] ) ) {
            $url = $args[ 'alias' ];
        }

        if ( isset( $args[ 'page' ] ) ) $url .= "{$this->setting[ 'rewrite_sep' ]}{$args[ 'page' ]}";
        $url .= $this->setting[ 'rewrite_suffix' ];
        if ( null != $anchor ) $url .= "#" . $anchor;
        return $this->setting[ 'weburl' ] . "/{$url}";
    }

    public function set_url() {
        if ( !$this->setting[ 'rewrite' ] ) return;

        $request = str_replace( $this->setting[ 'weburl' ], "", $_SERVER[ "REQUEST_URI" ] );
        $request = ltrim( $request, "\/\\" );

        if ( '?' == substr( $request, 0, 1 ) or 'index.php?' == substr( $request, 0, 10 ) ) return;
        if ( empty( $request ) or 'index.php' == $request ) return;

        $suffix = str_replace( ".", "\.", $this->setting[ 'rewrite_suffix' ] );

        foreach ( $this->setting[ 'rewrite' ] as $k => $v ) {
            $pattern = str_replace( array( '$alias', '$id', '$key', '$tag' ), array( "(.+)", "([0-9]+)", "(.+)", "(.+)" ), $v );
            $pattern = str_replace( array( '$y', '$m', '$d' ), "[0-9]+", $pattern );
            if ( $k != 'archives' ) {
                $pattern = "#{$pattern}{$this->setting[ 'rewrite_sep' ]}?([0-9]+)?{$suffix}$#";
            } else {
                $pattern = "#{$pattern}{$suffix}$#";
            }

            preg_match( $pattern, $request, $matches );
            if ( $matches ) {
                $_REQUEST[ 'c' ] = $k;
                $vars = array( '$alias', '$id', '$key', '$tag' );
                foreach ( $vars as $var ) {
                    if ( strpos( $v, $var ) !== false ) {
                        $arg = str_replace( '$', '', $var );
                        $_REQUEST[ $arg ] = $matches[ 1 ];
                        $_GET[ $arg ] = $matches[ 1 ];

                        if ( count( $matches ) > 2 && strpos( $pattern, $this->setting[ 'rewrite_sep' ] ) !== false ) {
                            $_REQUEST[ 'page' ] = end( $matches );
                            $_GET[ 'page' ] = end( $matches );
                        }
                        break;
                    }
                }
                return $k;
            }
        }

        /*
         *  首页分页解析
         */
        preg_match( "#page{$this->setting[ 'rewrite_sep' ]}([0-9]+){$suffix}$#", $request, $matches );
        if ( $matches ) {
            $_REQUEST[ 'c' ] = 'main';
            $_REQUEST[ 'page' ] = $matches[ 1 ];
            $_GET[ 'page' ] = $matches[ 1 ];
            return 'main';
        }

        /*
         *  文章别名解析
         */
        preg_match( "#(.+){$suffix}$#", $request, $matches );
        if ( $matches ) {
            $_REQUEST[ 'c' ] = 'archives';
            $_REQUEST[ 'alias' ] = $matches[ 1 ];
            $_GET[ 'alias' ] = $matches[ 1 ];
            return 'archives';
        }
        return false;
    }
}